﻿
<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加好友/群页面 - Layui</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link href="~/layui/css/pc/layim/layim.css" rel="stylesheet" />
    <link href="~/layui/css/pc/layer/layer.css" rel="stylesheet" />
    <link href="~/css/search.css" rel="stylesheet" />
    <script src="~/layui/layui.js"></script>

    <script>
        var userid = 0;
        layui.use(['jquery', 'laytpl', 'layer'], function (j, l, ly) {
            var $ = j;
            var laytpl = l;
            var layer = ly;
            userid = localStorage.getItem('currentUserId');
           
            var events = {
                getmsg: function () {
                    $.get('/layimapi/msg?t='+Math.random()+'&userid=' + userid, function (result) {
                        if (result.data) {
                            var gettpl = $('#template_msg_list').html();
                            laytpl(gettpl).render(result.data, function (html) {
                                $('#msglist').html(html);
                            });
                        }
                    });
                },
                handle: function (obj, reason) {
                    if (typeof reason === 'object') {
                        reason = '';
                    }
                    var o = {
                        userid: userid,
                        applyid: obj.data('id'),
                        reason: (reason || ''),
                        result: obj.data('result')
                    };
                    $.post('/layimapi/handle', o, function (result) {
                        if (result.code == 0) {
                            layer.alert('操作成功');
                            //刷新页面
                            events.getmsg();
                        }
                    });
                },
                refuse: function (obj) {
                    var html = '拒绝原因:<div class="layim-chat-textarea" style="margin-left:0px;"><textarea style="border:1px solid gray;width:100%;margin-top:5px;" maxlength="15" id="txtarea_otherinfo"></textarea></div>';
                    layer.confirm(html, {
                        title: '拒绝申请'
                   , shade: [0.8, '#393D49'],
                        yes: function (index) {
                            var reason = $('#txtarea_otherinfo').val();
                            events.handle(obj, reason);
                            layer.close(index);
                        }
                    });
                }
              
            };
            $(function () {
                events.getmsg();
                $(document).on('click', '*[data-event]', function (e) {
                    var othis = $(this);
                    var event = othis.data('event');
                    events[event] ? events[event].call(this, othis, e) : '';
                });
            });
          
        });
    </script>
</head>
<body>
    <div style="margin-top:10px;">

        <ul class="layui-layim-list" id="msglist">
        </ul>
</div>
</body>
</html>

<script type="text/html" id="template_msg_list">
        {{# for(var i = 0, len = d.length; i < len; i++){ }}
        <li>
            <img src="/headphotos/default.jpg">
            <span title="{{ d[i].applyname+(d[i].applyim) }}"> {{ d[i].applyname+(d[i].applyim) }} </span><span class="time-handled"> {{ d[i].addtime }} </span>
            <p> {{ d[i].msg }} </p>
            {{# if(d[i].result==0)  { }}
            <div class="btn-container">
                <button class="layui-form-button btn-add" data-result="1" data-type="{{ d[i].applytype }}" data-event="handle" data-userid="{{ d[i].userid }}"  data-id="{{ d[i].applyid }}">同意</button>
                <button class="layui-form-button btn-refuse" data-result="2" data-type="{{ d[i].applytype }}" data-event="refuse" data-userid="{{ d[i].userid }}" data-id="{{ d[i].applyid }}">拒绝</button>
                <button class="layui-form-button btn-ignore" data-result="3" data-type="{{ d[i].applytype }}" data-event="handle" data-userid="{{ d[i].userid }}" data-id="{{ d[i].applyid }}">忽略</button>
            </div>
            {{# } else{ }}
            {{# if(!d[i].isself)  { }}
            <div class="btn-container">
                <button class="layui-form-button btn-add">{{  d[i].result==1?'已同意':'已拒绝' }}</button>
            </div>
            {{# } }}
            {{# } }}
        </li>
        {{# } }}
</script>
